﻿using NUnit.Framework;
using Codeplus.Library.Models;
using Codeplus.Library.Controllers;
using Codeplus.Library.Web;
using Codeplus.Library.Data;
using Codeplus.Library.Constants;

namespace Codeplus.UnitTests
{
    [TestFixture]
    public class ContactUsFixture
    {
        private const string LocationData = @"<HostipLookupResultSet 
        xmlns:gml=""http://www.opengis.net/gml"" 
        xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
        version=""1.0.1"" 
        xsi:noNamespaceSchemaLocation=""http://www.hostip.info/api/hostip-1.0.1.xsd"">
    <gml:description>This is the Hostip Lookup Service</gml:description>
    <gml:name>hostip</gml:name>
    <gml:boundedBy>
        <gml:Null>inapplicable</gml:Null>
    </gml:boundedBy>
    <gml:featureMember>
        <Hostip>
            <ip>122.172.41.147</ip>
            <gml:name>Bangalore</gml:name>
            <countryName>INDIA</countryName>
            <countryAbbrev>IN</countryAbbrev>
            <!-- Co-ordinates are available as lng,lat -->
            <ipLocation>
                <gml:pointProperty>
                    <gml:Point srsName=""http://www.opengis.net/gml/srs/epsg.xml#4326"">
                        <gml:coordinates>77.5833,12.9833</gml:coordinates>
                    </gml:Point>
                </gml:pointProperty>
            </ipLocation>
        </Hostip>
    </gml:featureMember>
</HostipLookupResultSet>";
//        private const string LocationData =
//            @"<?xml version=""1.0"" encoding=""ISO-8859-1"" ?>
//            <HostipLookupResultSet version=""1.0.0"" 
//                xmlns=""http://www.hostip.info/api"" 
//                xmlns:gml=""http://www.opengis.net/gml"" 
//                xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
//                xsi:schemaLocation=""http://www.hostip.info/api/hostip-1.0.0.xsd"">
//             <gml:description>This is the Hostip Lookup Service</gml:description>
//             <gml:name>hostip</gml:name>
//             <gml:boundedBy>
//                <gml:Null>inapplicable</gml:Null>
//             </gml:boundedBy>
//             <gml:featureMember>
//                <Hostip>
//                 <gml:name>Sugar Grove, IL</gml:name>
//                 <countryName>UNITED STATES</countryName>
//                 <countryAbbrev>US</countryAbbrev>
//                 <!-- Co-ordinates are available as lng,lat -->
//                 <ipLocation>
//                    <gml:PointProperty>
//                     <gml:Point srsName=""http://www.opengis.net/gml/srs/epsg.xml#4326"">
//                        <gml:coordinates>-88.4588,41.7696</gml:coordinates>
//                     </gml:Point>
//                    </gml:PointProperty>
//                 </ipLocation>
//                </Hostip>
//             </gml:featureMember>
//            </HostipLookupResultSet>";

        [Test]
        public void ShouldSaveNewEnquiry()
        {
            var model = new ContactUsModel();
            model.FirstName = "sanjeet";
            model.LastName = "sahay";
            model.EmailAddress = "sanjeet.sahay@gmail.com";
            model.PhoneNumber = "+919980955558";
            model.ServiceCode = "web";
            var controller = new Contactus();
            var locationData = Geolocation.Parse(LocationData);
            controller.CollectContactInfo(model, "216.234.139.9", locationData);
        }

        [Test]
        public void ShouldParseLocationData()
        {
            var locationData = Geolocation.Parse(LocationData);
            Assert.AreEqual("IN", locationData.CountryCode);
        }

        [Test]
        public void ShouldRetrieveLocationData()
        {
            var geolocation = new Geolocation();
            var location = geolocation.GetLocation("216.234.139.9");
            Assert.IsNotNull(location);
        }

        [Test]
        public void ShouldChangeDatabaseConnectionVarsInRelease()
        {
            var dbInfo = AppDataContext.GetDatabaseInfo();
            if(Config.DebugMode)
            {
                Assert.AreEqual("webuser", dbInfo.UserName);    
            }
            else
            {
                Assert.AreEqual("codepluswebuser", dbInfo.UserName);    
            }
        }
    }
}
